<script lang="ts" setup>
import { ref, reactive, watch } from "vue";
import { onLoad,onUnload } from "@dcloudio/uni-app";
import { useRouter } from "uni-mini-router";

import covertContactMessage from "@/wfc/messages/covertContactMessage";
import store from "@/store";
import QuoteInfo from "@/wfc/model/quoteInfo";
import wfc from "@/wfc/client/wfc";
import Draft from "../../util/draft";
import { userStore } from "@/pinia";
import { getInteractionDic, setImInteraction } from "@/common/im";

import {
	req
} from '@/common/vmeitime-http/'
import api from '@/common/api.js'
const router = useRouter();

let address = ref("");
let phone = ref("");
let dfName = ref("")
let jobid = ref("");
let jobName = ref("");
let dfid = ref("");
let companyName = ref("");
let moneny = ref("");
let hr = ref(null)
const datetimesingle= ref('')
const userStoreInfo = userStore();
dfid.value = userStoreInfo.dfid
const userFlag = userStoreInfo.userFlag;//0 求职者，1，招聘方
let par ={
	paramType:10,
	paramValue:userStoreInfo.userInfo.id,
	remark:dfid.value
}
if(userFlag){
	par.paramValue = dfid.value
	par.remark = userStoreInfo.userInfo.id
}
req('GET',api.meiyeRecruitHunterInfo2Details,{
		userId: dfid.value
	}).then(res=>{
	console.log('我的个人信息是',res);
	dfName.value = res.result.realName
	
})
req('GET',api.getJobId,par).then(res=>{
	jobid = res.result.records[res.result.records.length-1].paramCode
	req('POST', api.jobContact2, {
			jobId:jobid,
			hunterId:dfid.value,
		}).then(res=>{
			console.log('让我看看面试的信息是啥',res);
			companyName.value = res.result.companyName
			jobName.value = res.result.job.jobName
			moneny.value = res.result.job.comprehensiveSalaryType==1?'面议':`${res.result.job.comprehensiveSalaryStart}-${res.result.job.comprehensiveSalaryEnd}`
			hr.value = res.result.hr
		})
})

onLoad((options) => {
  if (options.address) {
    uni.setStorageSync("address", options);
    address.value = options.address + options.addressDetail;
  } else if (options.user) {
    uni.setStorageSync("phone", options);
    phone.value = options.user + "·" + options.phone;
  }
  console.log(options);
});
onUnload(()=>{
	uni.setStorageSync("address", null);
	uni.setStorageSync("phone", null);
})
const info = reactive({
  time: null,
  text: "",
});
const ad = uni.getStorageSync("address");
const userIn = uni.getStorageSync("phone");
info.time = uni.getStorageSync("interviewTime");
info.text = uni.getStorageSync("interviewText")||'';
address.value = ad ? ad.address + ad.addressDetail : "";
phone.value = userIn ? userIn.user + "·" + userIn.phone : "";

const pickLists = reactive({
  time: ["10:00", "11:00", "15:00"],
});



const pickerShow = ref(false);
const pickerList = ref([]);

const showList = (e) => {
  pickerList.value = [];
  pickerShow.value = e;
  pickerList.value.push(pickLists[e]);
};

const pickerConfirm = (e) => {
  console.log(e.value[0]);
  info[pickerShow.value] = e.value[0];
  pickerShow.value = false;
};

const changeAddress = () => {
  router.push({
    name: "interviewAddress",
    params: {},
  });
};
const changePhone = () => {
  router.push({
    name: "interviewContact",
    params: {},
  });
};

const sendMessage = async () => {
	if(!companyName.value){
		uni.showToast({
			title: '未查询到公司信息',
			icon:'error'
		})
		return
	}
  const obj = {
    address: address.value,
    phone: phone.value,
    time: info.time,
    remark: info.text,
	companyName:companyName.value,
	jobName:jobName.value,
	moneny:moneny.value,
	hr:hr.value
  };
  const field = getInteractionDic("notice");
  let phoneMessageContent;
  phoneMessageContent = new covertContactMessage("interview", JSON.stringify(obj));
  setImInteraction(field, 1,jobid.value,dfid.value);
  // let phoneMessageContent = new TextMessageContent('15631143241');
  let quotedMessage = store.state.conversation.quotedMessage;
  if (quotedMessage) {
    let quoteInfo = QuoteInfo.initWithMessage(quotedMessage);
    phoneMessageContent.setQuoteInfo(quoteInfo);
    store.quoteMessage(null);
  }
  let currentConversation =
    store.state.conversation.currentConversationInfo.conversation;
  console.log({
    currentConversation,
  });
  wfc.sendConversationMessage(currentConversation, phoneMessageContent);
  Draft.setConversationDraft(currentConversation, "", null);
  uni.setStorageSync("address", null);
  uni.setStorageSync("phone", null);
  router.push({
    name: "ConversationPage",
    params: {},
  });
};
const changeLog = (e)=>{
	uni.setStorageSync("interviewTime", e);
}
watch(
      () => info.text,
      (v) => {
		  uni.setStorageSync("interviewText", v);
      },
      { deep: true },
    )
</script>

<template>
  <u-navbar
    :placeholder="true"
    bgColor="rgb(238,238,238)"
    leftText=""
    title=""
    auto-back
  >
  </u-navbar>
  <view class="main">
    <view class="title"> 邀请{{dfName}}加线下面试 </view>
    <view class="list">
      <view class="function">
        <text class="title"> 面试职位 </text>
        <view class="subTitle">
          <text>{{jobName}}·{{moneny}}</text>
        </view>
      </view>
      <view class="function" @click="changeAddress">
        <text class="title"> 面试地址 </text>
        <view class="funcItem">
          <text>{{ address }}</text>
          <u-icon name="arrow-right"></u-icon>
        </view>
      </view>
      <view class="function" @click="changePhone">
        <text class="title"> 联系人 </text>
        <view class="funcItem">
          <text>{{ phone }}</text>
          <u-icon name="arrow-right"></u-icon>
        </view>
      </view>
      <view class="function">
		  <uni-datetime-picker v-model="info.time" @change="changeLog">
			<view  style="display: flex; justify-content: space-between;">
				<text class="title"> 面试时间 </text>
				<view class="funcItem">
				  <text>{{ info.time }}</text>
				  <u-icon name="arrow-right"></u-icon>
				</view>
			</view>
		  </uni-datetime-picker>
      </view>
      <view
        class="function"
        style="flex-direction: column; align-items: flex-start"
      >
        <text class="title"> 备注事项 </text>
        <view style="width: inherit">
          <u--textarea
            v-model="info.text"
            placeholder="请输入内容"
            border="surround"
            maxlength="500"
            count
          ></u--textarea>
        </view>
      </view>
    </view>
    <u-picker
      :show="(pickerShow != false) == true&&pickerShow!=='time'"
      :columns="pickerList"
      @cancel="pickerShow = false"
      @confirm="pickerConfirm"
    >
    </u-picker>
    <view class="subBtn" @click="sendMessage"> 发送面试邀请 </view>
  </view>
</template>

<style lang="scss">
page {
  background-color: rgb(238, 238, 238);
}
.u-textarea {
  background-color: rgb(242, 242, 242);
  margin: 10rpx 0;
}

.main {
  display: flex;
  justify-content: center;
  flex-direction: column;
  align-items: center;

  .title {
    font-size: 50rpx;
    text-align: center;
    white-space: nowrap;
  }

  .list {
    width: 96%;
    box-sizing: border-box;
    margin: 50rpx auto 0;

    .function {
      padding: 25rpx;
      box-sizing: border-box;
      background-color: #fff;
      margin: 2rpx auto;
      width: 100%;
      display: flex;
      justify-content: space-between;
      align-items: center;

      .title {
        font-size: 30rpx;
        font-weight: 400;
      }

      .subTitle {
        text {
          margin-right: 10rpx;
          font-size: 28rpx;
          font-weight: 400;
          color: rgb(170, 170, 170);
        }
      }

      .funcItem {
        display: flex;
        align-items: center;

        text {
          margin-right: 10rpx;
          font-size: 28rpx;
          font-weight: 400;
          color: rgb(130, 130, 130);
        }
      }
    }
  }
  .subBtn {
    width: 600rpx;
    height: 80rpx;
    line-height: 80rpx;
    background-color: #d43951;
    color: #fff;
    font-size: 26rpx;
    border-radius: 8rpx;
    text-align: center;
    position: absolute;
    bottom: 0;
    margin-bottom: 30rpx;
  }
}
</style>
